/*
 * Project: purple
 * 
 * File Created at 2018年12月8日
 * 
 */
package xin.mydreams.purple.constant.resp;

import java.nio.charset.StandardCharsets;

import org.apache.commons.lang3.StringUtils;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import xin.mydreams.purple.constant.PurpleConstant;

/**
 * HTTP返回
 * 
 * @Type PurpleHttpResponse.java
 * @email guzehao@mydreams.xin
 * @author 谷泽昊
 * @blog https://www.mydreams.xin
 * @date 2018年12月8日 上午11:23:34
 * @version
 */
public class PurpleHttpResponse {

	/**
	 * 发送json数据
	 *
	 * @date Created at 2018年12月8日 下午2:43:25
	 * @emain guzehao@mydreams.xin
	 * @blog https://www.mydreams.xin
	 * @author 谷泽昊
	 * @param ctx
	 * @param content
	 */
	public static void sendJson(ChannelHandlerContext ctx, String json) {
		if(StringUtils.isBlank(json)) {
			json="没有内容";
		}
		ByteBuf buf = Unpooled.wrappedBuffer(json.getBytes(StandardCharsets.UTF_8));
		DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
				buf);
		HttpHeaders headers = response.headers();
		headers.setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
		headers.set(HttpHeaderNames.CONTENT_TYPE, PurpleConstant.ContentType.JSON);
		ctx.writeAndFlush(response);
	}
}

/**
 *
 * Revision history
 * -------------------------------------------------------------------------
 * 
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2018年12月8日 谷泽昊 creat
 */